package soul.exception;

import soul.domain.code.Status;
import soul.domain.response.Response;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by admin on 2016/11/17.
 */
@ControllerAdvice(basePackages = {"com.furious.soul.resource","com.furious.soul.filter"})
public class WebExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    Response handleControllerException(HttpServletRequest request, Throwable ex) {
        return Response.ok(Status.fail, ex.getMessage());
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }
}
